package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.report.lookanalyse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkKnowReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskQues;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkKnowReportRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskQuesRepository;

/**
 * 家长端作业系统 查看分析
 */
@CrossOrigin
@RestController
public class HomeworkReportAnalyseV1Controller extends YxyApiBaseController {

    @Autowired
    private HomeworkKnowReportRepository homeworkKnowReportRepository;

    @Autowired
    private HomeworkTaskQuesRepository homeworkTaskQuesRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workinfo/report/analyse.v1", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess( //
            @RequestParam String taskId, //
            @RequestParam String classId, //
            @RequestParam String studentId, //
            RespModel respModel) {
        // 知识点名称
        List<String> knowNamesList = new ArrayList<String>();
        // 知识点掌握情况
        List<HomeworkTaskQues> homeworkTaskQuesLst = homeworkTaskQuesRepository.findByTaskId(taskId);
        Map<Integer, String> knowMap = new HashMap<Integer, String>();
        homeworkTaskQuesLst.forEach(homeworkTaskQues -> {
            List<Integer> knowIds = homeworkTaskQues.getKnowId();
            List<String> knowNames = homeworkTaskQues.getKnowName();
            if (knowIds != null && knowIds.size() > 0) {
                for (int i = 0; i < knowIds.size(); i++) {
                    knowMap.put(knowIds.get(i), knowNames.get(i));
                }
            }
        });
        Set<Integer> knowIdSet = knowMap.keySet();
        // 当前学生在当前任务中的所有知识点的报告列表
        List<Long> studentrate = new ArrayList<Long>();
        for (Integer knowId : knowIdSet) {
            knowNamesList.add(knowMap.get(knowId));
            // 查出每个知识点下的个人的掌握率
            HomeworkKnowReport oneManOneKnowHomeworkKnowReport = homeworkKnowReportRepository
                    .findByStudentIdAndTaskIdAndKnowId(studentId, taskId, knowId);
            if (oneManOneKnowHomeworkKnowReport != null) {
                studentrate.add(oneManOneKnowHomeworkKnowReport.getRate().longValue());
            } else {
                studentrate.add(0l);
            }
        }
        // 3.通过班级id获取当前班级的所有的学生答题记录
        List<HomeworkKnowReport> homeworkKnowReports = homeworkKnowReportRepository.findByClassIdAndTaskId(classId,
                taskId);
        // 4.将班级id获取当前班级的所有的学生答题记录根据知识点id进行分组
        // 获取每个知识点总共的掌握率
        Map<Object, Double> result = homeworkKnowReports.parallelStream().filter(p -> p.getKnowId() != null)
                .collect(Collectors.groupingBy(p -> p.getKnowId(), Collectors.summingDouble(p -> p.getRate())));
        // 获取每个知识点出现的次数
        Map<Object, Integer> resultInt = homeworkKnowReports.parallelStream().filter(p -> p.getKnowId() != null)
                .collect(Collectors.groupingBy(p -> p.getKnowId(), Collectors.summingInt(p -> 1)));
        // 求出每个班级每个知识点的平均掌握率
        List<Long> classrate = new ArrayList<Long>();
        for (Integer knowId : knowIdSet) {
            for (Object key : result.keySet()) {
                if ((int) key == knowId) {
                    for (Object key2 : resultInt.keySet()) {
                        if (key2 == key) {
                            // 5. 获取每个知识点总共的掌握率除以获取每个知识点出现的次数==班级掌握率
                            Double classRate = result.get(key) / resultInt.get(key2);
                            // 6.将每个知识点的班级掌握率依次封装在classrate数组里
                            classrate.add(classRate.longValue());
                        }
                    }

                }
            }
        }
        // 封装知识点
        respModel.knowNamelList = knowNamesList;
        // 个人掌握率
        respModel.studentrate = studentrate;
        // 班级掌握率
        respModel.classrate = classrate;
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 知识点
        public List<String> knowNamelList;
        // 个人掌握率
        public List<Long> studentrate;
        // 班级掌握率
        public List<Long> classrate;
    }
}
